//
//  YTimePicker.swift
//  friends
//
//  Created by xwh on 2021/4/8.
//

import UIKit

class YTimePicker: UIViewController {

    @IBOutlet weak var containerView: UIView!
    @IBOutlet weak var pickerView: UIPickerView!
    
    var startYear:Int = 1950
    
    var currentDate:Date? = nil
    var completeDate:((Int,Int,Int)->())? = nil
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0)
        self.modalPresentationStyle = .custom
        
        if let ctd:Date = currentDate {
           
            let com = YDateUtil.getComponent(date:  ctd, timeCom: [.year,.month,.day])
            pickerView.selectRow((com.year ?? 0) - startYear, inComponent: 0, animated: true)
            pickerView.selectRow((com.month ?? 0) - 1, inComponent: 1, animated: true)
            pickerView.selectRow((com.day ?? 0) - 1, inComponent: 2, animated: true)
            
            pickerView.reloadAllComponents()
            
        }else{
            // 现在的
            let date_n:String = YDateUtil.getDateNow(format: "yyyy-MM-dd")
            if let date:Date = YDateUtil.getDate(time: date_n, format: "yyyy-MM-dd") {
                let com = YDateUtil.getComponent(date:  date, timeCom: [.year,.month,.day])
                pickerView.selectRow((com.year ?? 0) - startYear, inComponent: 0, animated: true)
                pickerView.selectRow((com.month ?? 0) - 1, inComponent: 1, animated: true)
                pickerView.selectRow((com.day ?? 0) - 1, inComponent: 2, animated: true)
                
                pickerView.reloadAllComponents()
            }
            
        }
        
        // Do any additional setup after loading the view.
    }
    

    @IBAction func cancelAction(_ sender: Any) {
        self.dismiss(animated: true, completion: nil)
        
    }
    
    
    @IBAction func submitAction(_ sender: Any) {
        let year:Int = pickerView.selectedRow(inComponent: 0) + startYear
        let month:Int = pickerView.selectedRow(inComponent: 1) + 1
        let day:Int = pickerView.selectedRow(inComponent: 2) + 1

        if completeDate != nil {
            completeDate!(year,month,day)
        }
        
        self.dismiss(animated: true, completion: nil)

        
    }
    
}

extension YTimePicker:UIViewControllerTransitioningDelegate {
    
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        let animation = YTransitionAnimation(type: .dismiss)
        return animation
    }
    
    
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        let animation = YTransitionAnimation(type: .present)
        return animation
    }
    
}

extension YTimePicker:UIPickerViewDelegate,UIPickerViewDataSource {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 3
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        
        if component == 0 {
            
            let com:DateComponents = YDateUtil.getComponent(date: Date(), timeCom: [.year])
            return (com.year ?? startYear) - startYear + 1
    
        }else if component == 1 {
            return 12
        }
        
        if let ctd:Date = currentDate {
            // 现在的
                let com = YDateUtil.getComponent(date:  ctd, timeCom: [.year,.month])
                let days:Int = YDateUtil.days(year: com.year ?? startYear, month: com.month ?? 1)
                return days
            
        }else{
            // 现在的
            let date_n:String = YDateUtil.getDateNow(format: "yyyy-MM-dd")
            if let date:Date = YDateUtil.getDate(time: date_n, format: "yyyy-MM-dd") {
                let com = YDateUtil.getComponent(date:  date, timeCom: [.year,.month])
                let days:Int = YDateUtil.days(year: com.year ?? startYear, month: com.month ?? 1)
                return days
            }
        }
        
        return 0
    }
    
    func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
        return UIScreen.main.bounds.width / 3
    }
    
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 40
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if component == 0 {
            return "\(startYear + row)" + "年"
        }
        
        return "\(row + 1)" + (component == 1 ? "月" : "日")
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if component == 1 ||  component == 0{
            pickerView.reloadComponent(2)
        }
    }
    
    
    
}
